const db = require('../db/index')


//查询报账分类表 
exports.getType = (req, res) => {
    db.query('select * from reimbursement_types', (err, result) => {
        if (err) return res.send({
            code: 500,
            message: err,
            data: {}
        })

        res.send({
            code: 200,
            message: '查询成功',
            data: result
        })
    })
}

//新增
exports.addType = (req, res) => {
    console.log(req.body)
    db.query('select * from reimbursement_types where type=?', req.body.type, (err, result) => {
        if (err) return res.send({
            code: 500,
            message: err,
            data: {}
        })
        if (result.length > 0) return res.send({
            code: 500,
            message: '分类名重复',
            data: {}   
        })
        db.query('insert into reimbursement_types set?', { type: req.body.type }, (err, result) => {
            if (err) return res.send({
                code: 500,
                message: err,
                data: {}
            })

            if (result.affectedRows !== 1) return res.send({
                code: 500,
                message: '新增失败',
                data: {}
            })

            res.send({
                code: 200,
                message: '新增成功',
                data: {}
            })
        })
    })

}

//修改
exports.editType = (req, res) => {
    db.query('update reimbursement_types set? where id=?', [req.body,req.body.id], (err, result) => {
        if (err) return res.send({
            code: 200,
            message: err,
            data: {}
        })
        if (result.affectedRows !== 1) return res.send({
            code: 500,
            message: '修改失败',
            data: {}
        })
        res.send({
            code: 200,
            message: '修改成功',
            data: {}
        })

    })
}

//删除
exports.deleteType = (req, res) => {
    db.query('delete from reimbursement_types where id=?', req.body.id, (err, result) => {
        if (err) return res.send({
            code: 500,
            message: err,
            data: {}
        })

        if (result.affectedRows<0) return res.send({
            code: 500,
            message: err,
            data: {}
        })
        
        res.send({
            code:200,
            message:"删除成功",
            data:{}
        })


    })
}